/**
 * Created by zhangbo on 11/12/17.
 */

var MiniMap = (function () {

    function _(minimapContainer, cesiumViewer, opt) {
        var options = this._options = opt || {width: '100%', height: '100%'};

        // Creating the container and stopping events from spilling through to the main map.
        this._container = document.getElementById(minimapContainer);
        this._container.style.width = options.width;
        this._container.style.height = options.height;
//        this._container.style.position = 'absolute';
//        this._container.style.right = '10px';
//        this._container.style.bottom = '10px';
//        this._container.style.margin = '10px';
//        this._container.style.boxShadow = '0 0 2px #777777';
//        this._container.style.borderRadius = '4px';
//        this._container.style.border = '2px solid #ff8000';

        var LMap = this._miniMap = L.map(minimapContainer, {
            center: [31.59, 120.29],
            zoom: 12,
            attributionControl: false,
            zoomControl: false,
            doubleClickZoom: false,
            dragging: false,
            boxZoom: false,
            scrollWheelZoom: false,
        });
//        L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?{foo}', {foo: 'bar'}).addTo(LMap);
        L.tileLayer('http://t{s}.tianditu.cn/DataServer?T=vec_w&X={x}&Y={y}&L={z}', {subdomains: ['0', '1', '2', '3', '4', '5', '6', '7']}).addTo(LMap);
        L.tileLayer('http://t{s}.tianditu.cn/DataServer?T=cva_w&X={x}&Y={y}&L={z}', {subdomains: ['0', '1', '2', '3', '4', '5', '6', '7']}).addTo(LMap);
        // L.tileLayer.chinaProvider('TianDiTu.Normal.Map',{maxZoom:18,minZoom:5}).addTo(map);
        // L.tileLayer.chinaProvider('TianDiTu.Normal.Annotion',{maxZoom:18,minZoom:5}).addTo(map);
        // TianDiTu: {
        //     Normal: {
        //         Map: "http://t{s}.tianditu.cn/DataServer?T=vec_w&X={x}&Y={y}&L={z}",
        //             Annotion: "http://t{s}.tianditu.cn/DataServer?T=cva_w&X={x}&Y={y}&L={z}",
        //     },
        //     Satellite: {
        //         Map: "http://t{s}.tianditu.cn/DataServer?T=img_w&X={x}&Y={y}&L={z}",
        //             Annotion: "http://t{s}.tianditu.cn/DataServer?T=cia_w&X={x}&Y={y}&L={z}",
        //     },
        //     Terrain: {
        //         Map: "http://t{s}.tianditu.cn/DataServer?T=ter_w&X={x}&Y={y}&L={z}",
        //             Annotion: "http://t{s}.tianditu.cn/DataServer?T=cta_w&X={x}&Y={y}&L={z}",
        //     },
        //     Subdomains: ['0', '1', '2', '3', '4', '5', '6', '7']
        // },

        var viewer = this._viewer = cesiumViewer;
        viewer.camera.moveEnd.addEventListener(this.cameraMoveEnd, this);

        var self = this;
    };

    _.prototype.cameraMoveEnd = function (e) {
        var camera = this._viewer.scene.camera;
        // Find the distance between two pixels at the bottom center of the screen.
        var width = this._viewer.scene.canvas.clientWidth;
        var height = this._viewer.scene.canvas.clientHeight;

//        var left = camera.getPickRay(new Cesium.Cartesian2((width / 2) | 0, height - 1));
//        var right = camera.getPickRay(new Cesium.Cartesian2(1 + (width / 2) | 0, height - 1));
        const center = camera.getPickRay(new Cesium.Cartesian2(
        		width / 2,
        		height / 2));
        const rightDown = camera.getPickRay(new Cesium.Cartesian2(width, height));
        const bottom = camera.getPickRay(new Cesium.Cartesian2(width/2, height));

        var scene = this._viewer.scene;
        var globe = this._viewer.scene.globe;
//        var leftPosition = globe.pick(left, scene);
//        var rightPosition = globe.pick(right, scene);
        var centerPosition = globe.pick(center, scene) || scene.camera.pickEllipsoid(new Cesium.Cartesian2(width/2, height/2));
        var rightDownPosition = globe.pick(rightDown, scene) || scene.camera.pickEllipsoid(new Cesium.Cartesian2(width, height));
        var bottomPosition = globe.pick(bottom, scene) || scene.camera.pickEllipsoid(new Cesium.Cartesian2(width/2, height));
        
        if(!Cesium.defined(centerPosition) || !Cesium.defined(rightDownPosition) || !Cesium.defined(bottomPosition)){
        	return;
        }
        
//        console.log(this.computeSignedTiltAngleOnGlobe(scene));
//        var leftCartographic = globe.ellipsoid.cartesianToCartographic(leftPosition);
//        var rightCartographic = globe.ellipsoid.cartesianToCartographic(rightPosition);
        var centerCartographic = globe.ellipsoid.cartesianToCartographic(centerPosition);
        var rightDownCartographic = globe.ellipsoid.cartesianToCartographic(rightDownPosition);
        var bottomCartographic = globe.ellipsoid.cartesianToCartographic(bottomPosition);

        if (Cesium.defined(centerCartographic) && Cesium.defined(rightDownCartographic) && Cesium.defined(bottomCartographic)) {
            var diffLat1 = Math.abs(Cesium.Math.toDegrees(rightDownCartographic.latitude) - Cesium.Math.toDegrees(centerCartographic.latitude));
            var diffLon1 = Math.abs(Cesium.Math.toDegrees(rightDownCartographic.longitude) - Cesium.Math.toDegrees(centerCartographic.longitude));
            var diffLat2 = Math.abs(Cesium.Math.toDegrees(bottomCartographic.latitude) - Cesium.Math.toDegrees(centerCartographic.latitude));
            var diffLon2 = Math.abs(Cesium.Math.toDegrees(bottomCartographic.longitude) - Cesium.Math.toDegrees(centerCartographic.longitude));
            var diffLat = Math.max(diffLat1, diffLat2);
            var diffLon = Math.max(diffLon1, diffLon2);
            var bounds = [
                [Math.max(Cesium.Math.toDegrees(centerCartographic.latitude) - diffLat, -90), Math.max(Cesium.Math.toDegrees(centerCartographic.longitude) - diffLon, -180)],
                [Math.min(Cesium.Math.toDegrees(centerCartographic.latitude) + diffLat, 90), Math.min(Cesium.Math.toDegrees(centerCartographic.longitude) + diffLon, 180)],
            ];
            var boundsx2 = [
                [Math.max(Cesium.Math.toDegrees(centerCartographic.latitude) - diffLat * 2, -90), Math.max(Cesium.Math.toDegrees(centerCartographic.longitude) - diffLon * 2, -180)],
                [Math.min(Cesium.Math.toDegrees(centerCartographic.latitude) + diffLat * 2, 90), Math.min(Cesium.Math.toDegrees(centerCartographic.longitude) + diffLon * 2, 180)],
            ];
            
            this._miniMap.fitBounds(boundsx2);
//            console.log(boundsx2);
            if (this._centerReact) {
                this._centerReact.setBounds(bounds);
            }
            else {
                this._centerReact = L.rectangle(bounds, {color: "#ff0000", weight: 1}).addTo(this._miniMap);
            }
            
            if (this._centerMarker) {
                this._centerMarker.setLatLng([Cesium.Math.toDegrees(centerCartographic.latitude), Cesium.Math.toDegrees(centerCartographic.longitude)]);
            }
            else {
                this._centerMarker = L.marker([Cesium.Math.toDegrees(centerCartographic.latitude), Cesium.Math.toDegrees(centerCartographic.longitude)]).addTo(this._miniMap);
            }
        } else {

        }
        
//        if(Cesium.defined(this.centerP))this.centerP.show = false;
    };
    
    _.prototype.destroy = function () {
        viewer.camera.moveEnd.removeEventListener(this.cameraMoveEnd);
    };

    return _;
})();
